package com.pucheng.bracelet.app.base

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarLayout
import com.haibin.calendarview.CalendarView
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.util.click
import kotlinx.android.synthetic.main.activity_base_history_to_date.*
import kotlinx.android.synthetic.main.layout_title2.*
import me.hgj.jetpackmvvm.base.activity.BaseVmActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

abstract class BaseHistoryToDateActivity<VM : BaseViewModel> :
    BaseVmActivity<VM>(),
    CalendarView.OnCalendarSelectListener,
    CalendarView.OnYearChangeListener {
    var mTextMonthDay: TextView? = null
    var mCalendarView: CalendarView? = null

    private var mYear = 0
    var mCalendarLayout: CalendarLayout? = null

    override fun layoutId(): Int = R.layout.activity_base_history_to_date

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        tv_title.text = getString(titleResId())
        ibtn_back.click { finish() }
        mTextMonthDay = tv_month_day
        mCalendarLayout = calendarLayout
        mCalendarView = calendarView
        mTextMonthDay!!.setTextColor(resources.getColor(colorId()))
        mTextMonthDay!!.click {
            if (!mCalendarLayout!!.isExpand) {
                mCalendarLayout!!.expand()
                return@click
            }
            mCalendarView!!.showYearSelectLayout(mYear)
            mTextMonthDay!!.text = mYear.toString()
        }
        mCalendarView!!.setOnCalendarSelectListener(this)
        mCalendarView!!.setOnYearChangeListener(this)
        mCalendarView!!.setThemeColor(resources.getColor(colorId()), resources.getColor(colorId()))
        mYear = mCalendarView!!.curYear
        mTextMonthDay!!.text = mCalendarView!!.curYear.toString() + "年" +
                mCalendarView!!.curMonth.toString() + "月"
        val year = mCalendarView!!.curYear
        val month = mCalendarView!!.curMonth


    }

    abstract fun colorId(): Int
    fun getSchemeCalendar(
        year: Int,
        month: Int,
        day: Int,
        text: String
    ): Calendar? {
        val calendar = Calendar()
        calendar.year = year
        calendar.month = month
        calendar.day = day
//        calendar.schemeColor = color //如果单独标记颜色、则会使用这个颜色
        calendar.scheme = text
        return calendar
    }


    override fun onCalendarOutOfRange(calendar: Calendar?) {}

    @SuppressLint("SetTextI18n")
    override fun onCalendarSelect(
        calendar: Calendar,
        isClick: Boolean
    ) {
        mTextMonthDay!!.text =
            calendar.year.toString() + "年" + calendar.month.toString() + "月"
        mYear = calendar.year
    }

    override fun onYearChange(year: Int) {
        mTextMonthDay!!.text = year.toString()
    }

    abstract fun titleResId(): Int
    override fun createObserver() {
    }

    override fun dismissLoading() {
    }

    override fun showLoading(message: String) {
    }
}